package com.example.expandablelistview.multimedia;

import java.io.IOException;
import java.util.List;

import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class MyCameraSurface extends SurfaceView implements Callback {
	SurfaceHolder mHolder;
	Camera mCamera;
	
	public MyCameraSurface(Context context, AttributeSet attrs) {
		super(context, attrs);
		mHolder = getHolder();
		mHolder.addCallback(this);
	}

	public void surfaceCreated(SurfaceHolder holder) {
		mCamera = Camera.open();
		try {
			mCamera.setPreviewDisplay(mHolder);
		} catch (IOException e) {
			mCamera.release();
			mCamera = null;
		}
	}
	
	public void surfaceDestroyed(SurfaceHolder holder) {
		if (mCamera != null){
			mCamera.stopPreview();
			mCamera.release();
			mCamera = null;
		}
	}
	
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		Camera.Parameters params = mCamera.getParameters();
		List<Size> arSize = params.getSupportedPreviewSizes();
		
		if (arSize==null){
			params.setPreviewSize(width, height);
		}else{
			int diff = 10000;
			Size opti = null;
			
			for(Size s : arSize){
				if (Math.abs(s.height-height)< diff){
					diff = Math.abs(s.height-height);
					opti = s;
				}
			}
			params.setPictureSize(opti.width, opti.height);
		}
		 mCamera.setParameters(params);
		 mCamera.startPreview();
	}

	

}
